package com.dragon.manage.service.impl;

import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dragon.manage.bean.ManageUser;
import com.dragon.manage.bean.RightInfo;
import com.dragon.manage.dao.IManageUserDAO;
import com.dragon.manage.dao.IRightInfoDAO;
import com.dragon.manage.service.IManageRightService;
import com.dragon.manage.util.ManageUtil;
import com.dragon.manage.util.StaticData;
import com.dragon.manage.util.SystemParamsUtil;

public class ManageRightServiceImpl implements IManageRightService{

	/**
	 * 管理用户
	 */
	private IManageUserDAO manageUserDao;
	/**
	 * 权限管理DAO
	 */
	private IRightInfoDAO	rightInfoDAO;
	
	public void setManageUserDao(IManageUserDAO manageUserDao) {
		this.manageUserDao = manageUserDao;
	}

	public void setRightInfoDAO(IRightInfoDAO rightInfoDAO) {
		this.rightInfoDAO = rightInfoDAO;
	}


	@Override
	public JSONObject getManageUserList(int manageUserId, int page)throws Exception {
		JSONObject reJs = new JSONObject();
		List<ManageUser> list = manageUserDao.getManageUserList(manageUserId, page);
		JSONArray jsArray = new JSONArray();
		for(ManageUser tmp:list){
			jsArray.add(ManageUtil.getManageUserPage(tmp));
		}
		if(page!=StaticData.no_param){
			reJs.put(StaticData.reKey.total.getKey(), manageUserDao.getManageUserCount());
		}
		reJs.put(StaticData.reKey.code.getKey(), StaticData.codeType.ok.getType());
		reJs.put("manageUserList", jsArray);
		return reJs;
	}


	@Override
	public JSONObject updateManageUser(int manageUserId, int isRest,
			String userRight, int isDelete) throws Exception {
		JSONObject reJs = new JSONObject();
		/*
		 * 获取对应的用户信息
		 */
		ManageUser manageUser = manageUserDao.getManageUserByID(manageUserId);
		if(manageUser==null){
			reJs.put(StaticData.reKey.code.getKey(), StaticData.codeType.param_error.getType());
			return reJs;
		}
		/*
		 * 如果重置密码
		 */
		if(isRest!=StaticData.no_param){
			manageUser.setPwd(SystemParamsUtil.getManageInitPwd());
		}else if(userRight!=null){
			manageUser.setUserRight(userRight);
		}else if(isDelete!=StaticData.no_param){
			manageUser.setUserStatus(StaticData.userType.delete.getType());
		}
		manageUserDao.updateManageUser(manageUser);
		reJs.put(StaticData.reKey.code.getKey(), StaticData.codeType.ok.getType());
		return reJs;
	}


	@Override
	public JSONObject addManageUser(String manageUserName, String userRight)
			throws Exception {
		JSONObject reJs = new JSONObject();
		ManageUser manageUser = new ManageUser();
		manageUser.setUserName(manageUserName);
		if(userRight!=null){
			manageUser.setUserRight(userRight);
		}
		manageUser.setPwd(SystemParamsUtil.getManageInitPwd());
		manageUser.setUserStatus(StaticData.userType.normal.getType());
		manageUserDao.createManageUser(manageUser);
		reJs.put(StaticData.reKey.code.getKey(), StaticData.codeType.ok.getType());
		return reJs;
	}


	@Override
	public JSONObject getRightList() throws Exception {
		JSONObject reJs = new JSONObject();
		List<RightInfo> reMap =rightInfoDAO.getRightInfoList();
		if(reMap==null||reMap.size()==0){
			reJs.put(StaticData.reKey.code.getKey(), StaticData.codeType.param_error.getType());
			return reJs;
		}
		JSONArray jsArray = new JSONArray();
		JSONObject jsTmp = null;
		for(RightInfo tmp : reMap){
			jsTmp = new JSONObject();
			jsTmp.put("rName", tmp.getRName());
			jsTmp.put("rId", tmp.getId());
			jsArray.add(jsTmp);
		}
		reJs.put("rightList", jsArray);
		reJs.put(StaticData.reKey.code.getKey(), StaticData.codeType.ok.getType());
		return reJs;
	}

	
	
}
